package com.regentsoft.cloudback.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**
 * 跨域拦截
 * @author czd
 *
 * @date 2017年11月25日
 *
 * @version 1.0
 */
public class CORSInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 设置可跨域访问
		if(request.getHeader(HttpHeaders.ORIGIN) != null){
			response.setContentType("text/javascript; charset=utf-8");
			response.addHeader("Access-Control-Allow-Origin",
					"*"); // 测试环境
			response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, HEAD");
			response.addHeader("Access-Control-Allow-Credentials", "true");
			response.addHeader("Access-Control-Max-Age", "1000");
			response.addHeader("Access-Control-Allow-Headers","Content-Type"); 
		}
		return true;
	}
}
